package com.lrkj.web.phone;

import com.lrkj.common.core.domain.AjaxResult;
import com.lrkj.common.core.page.TableDataInfo;
import com.lrkj.common.utils.DateUtils;
import com.lrkj.common.utils.StringUtils;
import com.lrkj.framework.security.MiniContextUtils;
import com.lrkj.payfee.domain.BalanceRefill;
import com.lrkj.payfee.domain.RoomBalance;
import com.lrkj.payfee.domain.vo.BalanceRefillInVo;
import com.lrkj.payfee.domain.vo.RoomBalanceOutVo;
import com.lrkj.payfee.service.IBalanceRefillService;
import com.lrkj.payfee.service.IRoomBalanceService;
import com.lrkj.property.domain.Room;
import com.lrkj.waitress.domain.CustomInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.YearMonth;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author huxy
 * @version V1.0
 * @创建时间
 * @备注 余额 相关
 * @类名
 * @节点
 */
@RestController
@RequestMapping("/phone/balance")
public class BalanceController extends CommonUseBaseController {
    @Autowired
    private IBalanceRefillService balanceRefillService;
    @Autowired
    private IRoomBalanceService roomBalanceService;


    @PostMapping("recharge")
    public AjaxResult recharge(@Valid @RequestBody BalanceRefillInVo balanceRefillInVo) {
//        Room room = getRoom(balanceRefillInVo.getRoomId());
//        if (room == null) {
//            return AjaxResult.error("房间信息不存在！");
//        }
//        //获取客户信息
//        CustomInfo customInfo = getCustomInfo(room.getCommunityId());
//        if (customInfo == null) {
//            return AjaxResult.error("未绑定房屋信息！请先绑定");
//        }
//
//        BalanceRefill balanceRefill = getBalanceRefill(balanceRefillInVo);
//        // todo 微信支付成功后 赋值状态码
//        balanceRefill.setStatus("10002"); //参考其他逻辑 固定写死
//        balanceRefill.setReceiptsMoney(balanceRefill.getRechargeMoney());
//        balanceRefill.setCreateBy(customInfo.getCustomName());
//        balanceRefill.setCustomId(customInfo.getCustomId());
//        balanceRefill.setPayeeTime(DateUtils.getNowDate());
//        balanceRefill.setCustomName(customInfo.getCustomName());
//        balanceRefill.setCommunityId(room.getCommunityId());
//        balanceRefill.setPayeeType("WX");
//        balanceRefillService.insertBalanceRefill(balanceRefill);
//        return AjaxResult.success(balanceRefill.getId());
        return AjaxResult.success();
    }

    @GetMapping("list")
    public TableDataInfo rechargeRecord(@Valid BalanceRefillInVo balanceRefillInVo) {
        BalanceRefill balanceRefill = getBalanceRefill(balanceRefillInVo);
        Room room = getRoom(balanceRefillInVo.getRoomId());
        balanceRefill.setCommunityId(room.getCommunityId());
        if (balanceRefillInVo.getTime() != null && balanceRefillInVo.getTime() > 0) {
            balanceRefill.setEndDate(DateUtils.dateTimeNow("yyyy-MM"));
            YearMonth yearMonth = YearMonth.parse(balanceRefill.getEndDate());
            balanceRefill.setStartDate(yearMonth.minus(balanceRefillInVo.getTime(), ChronoUnit.MONTHS).toString());
        }
        startPage();
        List<BalanceRefill> list = balanceRefillService.selectBalanceRefillList(balanceRefill);
        return getDataTable(list);
    }

    @GetMapping("my/{roomId}&{type}")
    public AjaxResult account(@PathVariable("roomId") String roomId, @PathVariable("type") Integer type) {
        //当前APP用户的手机号码
        String phoneNum = MiniContextUtils.getPhoneNum();
        RoomBalance roomBalance = new RoomBalance();
        roomBalance.setRoomId(roomId);
        List<RoomBalance> list = roomBalanceService.selectRoomBalanceList(roomBalance);
        if (type != null) {
            double receiptsMoney = list.stream().map(a -> a.getReceiptsMoney().doubleValue()).collect(Collectors.toList()).stream().mapToDouble(Double::doubleValue).sum();
            return AjaxResult.success(receiptsMoney);
        }
        return AjaxResult.success(list.stream().map(a -> {
            RoomBalanceOutVo balanceOutVo = new RoomBalanceOutVo();
            BeanUtils.copyProperties(a, balanceOutVo);
            return balanceOutVo;
        }).collect(Collectors.toList()));
    }

    @GetMapping("rechargeInfo/{id}")
    public AjaxResult rechargeInfo(@PathVariable("id") String id) {
        if (StringUtils.isEmpty(id)) {
            return AjaxResult.error("ID 错误！");
        }
        return AjaxResult.success("ok", balanceRefillService.selectBalanceRefillById(id));
    }

    private BalanceRefill getBalanceRefill(BalanceRefillInVo balanceRefillInVo) {
        BalanceRefill balanceRefill = new BalanceRefill();
        BeanUtils.copyProperties(balanceRefillInVo, balanceRefill);
        return balanceRefill;
    }
}


